package io.bba.boot.app.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.bba.boot.framework.exception.BusinessException;
import io.bba.boot.framework.page.OrderByItem;
import io.bba.boot.framework.page.OrderMapping;
import io.bba.boot.framework.page.Paging;
import io.bba.boot.app.dto.ArticleDto;
import io.bba.boot.app.entity.Article;
import io.bba.boot.app.mapper.ArticleMapper;
import io.bba.boot.app.query.ArticleQuery;
import io.bba.boot.app.service.ArticleService;
import io.bba.boot.app.vo.ArticleVo;
import io.bba.boot.app.query.AppArticleQuery;
import io.bba.boot.app.vo.AppArticleVo;
import io.bba.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * 文章 服务实现类
 *
 * @author zs
 * @since 2024-03-22
 */
@Slf4j
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addArticle(ArticleDto dto) {
        Article article = new Article();
        BeanUtils.copyProperties(dto, article);
        return save(article);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateArticle(ArticleDto dto) {
        Long id = dto.getId();
        Article article = getById(id);
        if (article == null) {
            throw new BusinessException("文章不存在");
        }
        BeanUtils.copyProperties(dto, article);
        return updateById(article);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteArticle(Long id) {
        return removeById(id);
    }

    @Override
    public ArticleVo getArticleById(Long id) {
        return articleMapper.getArticleById(id);
    }

    @Override
    public Paging<ArticleVo> getArticlePage(ArticleQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<ArticleVo> list = articleMapper.getArticlePage(query);
        Paging<ArticleVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppArticleVo getAppArticleById(Long id) {
        return articleMapper.getAppArticleById(id);
    }

    @Override
    public Paging<AppArticleVo> getAppArticlePage(AppArticleQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppArticleVo> list = articleMapper.getAppArticlePage(query);
        Paging<AppArticleVo> paging = new Paging<>(list);
        return paging;
    }

}
